package history.hot100;

// 152. 乘积最大子数组:https://leetcode-cn.com/problems/maximum-product-subarray/
// 题解：https://leetcode-cn.com/problems/maximum-product-subarray/solution/hua-jie-suan-fa-152-cheng-ji-zui-da-zi-xu-lie-by-g/
public class LeetCode_152 {
    public int maxProduct(int[] nums) {
        int max = Integer.MIN_VALUE, imax=1, imin=1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < 0) {
                int tmp = imax;
                imax = imin;
                imin =tmp;
            }
            imax = Math.max(imax*nums[i],nums[i]);
            imin = Math.min(imin*nums[i],nums[i]);

            max = Math.max(max,imax);
        }
        return max;
    }
}
